package com.javasm.gm.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasm.gg.GoodsService;
import com.javasm.gm.*;
import com.javasm.gm.dao.Goods2GiftsDao;
import com.javasm.process.ProcessService;
import com.javasm.process.entity.ProcessTask;
import com.javasm.process.entity.TaskStatus;
import com.javasm.sys.CurrentLogin;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

@Service
public class Goods2GiftsServiceImpl extends ServiceImpl<Goods2GiftsDao, Goods2Gifts> implements Goods2GiftsService {
    @Resource
    private ProcessService processService;
    @Resource
    private GiftService giftService;
    @Resource
    private GoodsService goodsService;

    private static String defName="商品转赠品流程";

    @Override
    public Collection<Goods2Gifts> listGoods2GiftsApply(Goods2Gifts obj) {
        return this.baseMapper.listGoods2GiftsApply(obj);
    }

    @Transactional
    @Override
    public boolean submitApply(Goods2Gifts obj) {
        boolean b=false;
        if(StrUtil.isEmpty(obj.getId()))
            b = save(obj);
        else
            b=updateById(obj);

        b = processService.startProcess(defName, obj.getId());
        return b;
    }

    @Override
    public List<ProcessTask> getTaskByApplyOrderId(String orderId) {
        return processService.getTaskByApplyOrderId(defName,orderId);
    }

    @Override
    public List<ProcessTask> queryToDoTasks() {
        List<ProcessTask> tasks = processService.queryToDoTasks(defName, CurrentLogin.getLoginUserName());
        return tasks;
    }

    @Override
    public List<ProcessTask> queryDosTasks(String o) {
        List<ProcessTask> processTasks = processService.queryDosTasks(defName, CurrentLogin.getLoginUserName(), o);
        return processTasks;
    }

    @Override
    public boolean claim(String orderId) {
        return processService.claim(CurrentLogin.getLoginUserName(),orderId);
    }

    @Transactional
    @Override
    public boolean approve(ProcessTask task) {
        String result = processService.approve(defName, task, CurrentLogin.getLoginUserName());
        if (null == result) return true;

        //流程通过已结束或流程未通过
        Goods2Gifts cgcp = new Goods2Gifts();
        cgcp.setId(task.getApplyId());
        cgcp.setApplyStatus(result);
        updateById(cgcp);

        if(result.equals(TaskStatus.APPROVE_TG.getStatus())){
            //修改赠品数量,修改商品数量
            Goods2Gifts byId = getById(task.getApplyId());
            Integer count = byId.getCount();
            Integer goodsId = byId.getGoodId();
            goodsService.updateGoodsStock(goodsId,-count);

            Integer giftId = giftService.getGiftIdByGoodsId(goodsId);
            giftService.updateGiftStock(giftId,count);
        }
        return true;
    }

    @Override
    public Goods2Gifts getOneById(String id) {
        return baseMapper.getById(id);
    }
}

